package com.rbac.interfaces.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.rbac.application.service.MenuService;
import com.rbac.common.annotation.OperationLog;
import com.rbac.common.result.Result;
import com.rbac.domain.dto.response.MenuResponse;
import com.rbac.domain.entity.Menu;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "菜单管理", description = "菜单管理相关接口")
@RestController
@RequestMapping("/system/menu")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService menuService;

    @Operation(summary = "获取菜单树")
    @GetMapping("/tree")
    @SaCheckPermission("system:menu:query")
    public Result<List<Menu>> getMenuTree() {
        return Result.success(menuService.getMenuTree());
    }

    @Operation(summary = "获取用户菜单")
    @GetMapping("/user/{userId}")
    @SaCheckPermission("system:menu:query")
    public Result<List<MenuResponse>> getUserMenus(@PathVariable Long userId) {
        return Result.success(menuService.getUserMenus(userId));
    }

    @Operation(summary = "创建菜单")
    @PostMapping
    @OperationLog("创建菜单")
    @SaCheckPermission("system:menu:add")
    public Result<Menu> createMenu(@Validated @RequestBody Menu menu) {
        return Result.success("菜单创建成功", menuService.createMenu(menu));
    }

    @Operation(summary = "更新菜单")
    @PutMapping
    @OperationLog("更新菜单")
    @SaCheckPermission("system:menu:edit")
    public Result<Menu> updateMenu(@Validated @RequestBody Menu menu) {
        return Result.success("菜单更新成功", menuService.updateMenu(menu));
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    @OperationLog("删除菜单")
    @SaCheckPermission("system:menu:delete")
    public Result<Void> deleteMenu(@PathVariable Long id) {
        menuService.deleteMenu(id);
        return Result.success("菜单删除成功");
    }
}